<?php
class CategoryService extends BaseDbService
{
	public function __construct()
	{
		$this->dao	= new BaseDao('category');
	}
	
	public function getParentCategoryList($id, $self=true)
	{
		$list	= array();
		$info	= $this->dao->get($id);
		if($info)
		{
			if($info['pid']>0)
			{
				$list = $this->getParentCategoryList($info['pid'],true);
			}
			if($self)
			{
				if(!$list)
				{
					$list	= array();
				}
				$list[]	= $info;
			}
		}
		return $list;
	}
	
	public function hideCategory($id,$hide)
	{
		//隐藏或取消隐藏当前栏目
		$this->update($id,array('is_hide'=>$hide));
		
		//隐藏或取消隐藏下级栏目
		$subList	= $this->getCategoryListByPId($id);
		if($subList)
		{
			foreach($subList as $sv)
			{
				$this->hideCategory($sv['id'],$hide);
			}
		}
	}
	
	public function del($id)
	{
		$list = $this->getCategoryListByPId($id);
		if($list)
		{
			foreach($list as $lv)
			{
				$this->del($lv['id']);
			}
		}
		$this->dao->del($id);
	}
	
	public function checkIdAndPid($id,$pid)
	{
		$list = $this->getParentCategoryList($pid);
		if($list)
		{
			foreach($list as $lv)
			{
				if($lv['id']==$id)
				{
					return false;
				}
			}
		}
		return true;
	}
	
	public function checkPidLevel($pid, $maxLevel=2)
	{
		if($pid==0)
		{
			return true;
		}
		
		$list = $this->getParentCategoryList($pid);
		if(count($list)>$maxLevel)
		{
			return false;
		}
		
		return true;
	}
	
	private function getCategoryListByPId($pid)
	{
		return $this->rows(array('pid'=>$pid),'orderby DESC,id ASC');
	}
	
	public function getJsCategory()
	{
		$categories = array();
		
		$topCategories = $this->getCategoryListByPId(0);
		if(!empty($topCategories))
		{
			$categoryIds = Util::parseIds('id', $topCategories);
			$subCategories = $this->rows(array('pid'=>array('in'=>$categoryIds)), 'orderby DESC,id ASC');
		}
		
		if(empty($topCategories))
		{
			$topCategories = array();
		}
		
		if(empty($subCategories))
		{
			$subCategories = array();
		}
		
		$list = array_merge($topCategories, $subCategories);
		
		
		
		$all = array();
		$multi = array();
		foreach($list as $cv)
		{
			$tmp							= array('name'=>$cv['name'],'value'=>$cv['id'],'pid'=>$cv['pid']);
			$all[$cv['id']]					= $tmp;
			$multi['multi_'.$cv['pid']][]	= $tmp;
		}
	
		return array('dataAllCategory'=>$all,'dataMultiCategory'=>$multi);
	}
	
	public function getCateogryLevelsById($categoryId)
	{
		$topList = $this->rows(array('pid'=>0),'orderby DESC, id ASC');
		if(!$topList)
		{
			return false;
		}
		
		$categoryMap = Util::groupData($topList, array('id'));
		$cat = false;
		if($categoryId>0)
		{
			if(array_key_exists($categoryId, $categoryMap))
			{
				$cat = $categoryMap[$categoryId];
			}
			else
			{
				$cat = $this->get($categoryId);
			}
		}
		
		if(!$cat)
		{
			$default = true;
			$cat = $topList[0];
		}
		
		//判断是哪一级类目
		if($cat['pid']==0)
		{
			//顶级类目
			$secondList = $this->rows(array('pid'=>$cat['id']), 'orderby DESC, id ASC');
			if($secondList)
			{
				$thirdList = $this->rows(array('pid'=>$secondList[0]['id']), 'orderby DESC, id ASC');
			}
			$first = $default?0:$cat['id'];
			$second = 0;
			$third = 0;
		}
		else if(array_key_exists($cat['pid'], $categoryMap))
		{
			//二级类目
			$secondList = $this->rows(array('pid'=>$cat['pid']), 'orderby DESC, id ASC');
			$thirdList = $this->rows(array('pid'=>$cat['id']), 'orderby DESC, id ASC');
			
			$first = $cat['pid'];
			$second = $cat['id'];
			$third = 0;
		}
		else
		{
			//三级类目
			$thirdList = $this->rows(array('pid'=>$cat['pid']), 'orderby DESC, id ASC');
			$secondCat = $this->get($cat['pid']);
			$secondList = $this->rows(array('pid'=>$secondCat['pid']), 'orderby DESC, id ASC');
			$first = $secondCat['pid'];
			$second = $cat['pid'];
			$third = $cat['id'];
		}
		
		return array('topList'=>$topList,'secondList'=>$secondList,'thirdList'=>$thirdList, 'first'=>$first, 'second'=>$second, 'third'=>$third);
	}
}